package com.lancoo.znbkxx.teacher.aibkTestCoach.kBase

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.auidoPlayer.AudioPlayer
import com.lancoo.znbkxx.teacher.aibkTestCoach.auidoPlayer.AudioTracker
import com.lancoo.znbkxx.teacher.aibkTestCoach.auidoPlayer.ChildAudioPlayer

/**
 * fragment的基类
 */
abstract class KBaseFragment : Fragment() {
    //听力播放器
    protected var audioPlayer: AudioPlayer? = null
    protected var childAudioPlayer: ChildAudioPlayer? = null
    private var mView: View? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (mView == null) {
            mView = inflater.inflate(createLayout(), container, false)
            initView(mView)
        }
        return mView
    }

    protected abstract fun createLayout(): Int
    protected abstract fun initView(view: View?)

    @SuppressLint("UseRequireInsteadOfGet")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 如果savedInstanceState不为空，那么可以获取在onSaveInstanceState方法中保存的值。
        if (savedInstanceState != null) {
            val isHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN) // 获取保存fragment前的可视状态
            val transaction = fragmentManager!!.beginTransaction()
            if (isHidden) {
                // 如果原来fragment是隐藏状态，那么就hide
                transaction.hide(this)
            } else {
                // 如果原来fragment是显示状态，那么就show
                transaction.show(this)
            }

//            transaction.commit();
            transaction.commitAllowingStateLoss()
        }
    }

    /**
     * 该方法在onCreate之前调用，实现数据的预加载
     *
     * @param isVisibleToUser 界面对用户是否可见
     * false表示对用户不可见，反之为true
     */
    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            onVisible()
        } else {
            onInVisible()
        }
    }

    /**
     * 界面对用户不可见时，执行该方法
     */
    protected fun onInVisible() {}

    /**
     * 界面对用户可见时，执行加载数据的方法
     */
    protected fun onVisible() {
        setLazyLoad()
    }

    /**
     * 加载数据方法
     */
    protected fun setLazyLoad() {}


    /**
     * 调用该方法用来保存fragment当前的动态状态。
     * 可以用bundle对象保存一些值，然后可以在onCreate方法中获取保存的值。
     *
     * @param outState Bundle对象
     */
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden) // 保存当前fragment的可视状态
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (audioPlayer != null) {
            audioPlayer!!.DestoryAudio()
        }

//        if(childAudioPlayerA !=null){
//            childAudioPlayerA!!.DestoryChildAudio()
//        }
    }

    fun pauseAudio() {
        if (audioPlayer != null) {
            audioPlayer!!.pauseAudio()
        }
    }

    fun pauseChildAudio() {
        if(childAudioPlayer !=null){
            childAudioPlayer!!.pauseChildAudio()
        }
    }

    protected var audioTracker: AudioTracker? = null

    //暂停作文音频播放
    fun audioTrackerStop() {
        if (audioTracker != null) {
            audioTracker!!.stop()
        }
    }

    companion object {
        private const val STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN"
    }
}